package com.runa.monitor.platinterface.netservice.equipment.dtu.fourG;

import com.rn.smart.command.dto.valve.contro.RunaValve;
import com.runa.monitor.comm.dto.Node;
import com.runa.rccprotocol.netservice.dtu.ModbusRtuResponse;
import com.runa.rccprotocol.task.FourGValveTask;
import com.runa.rccprotocol.task.FourGValveWriteTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 2024/9/11 11:33
 * zhangYB
 */
@Slf4j
@Service
public class FourGValveDtuService implements ApplicationRunner {

    private static Map<String, FourGValveDtuDriver> driverMap = new ConcurrentHashMap<>();

    @Override
    public void run(ApplicationArguments args) throws Exception {
        Timer timer = new Timer("4G阀执行器");
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                try {
                    for (String key : driverMap.keySet()) {
                        FourGValveDtuDriver fourGValveDtuDriver = driverMap.get(key);
                        fourGValveDtuDriver.requestPost();
                    }
                } catch (Exception e) {
                    log.error("", e);
                }
            }
        }, 100, 100);
    }

    public FourGValveDtuDriver getFourGValveDtuDriver(String key) {
        FourGValveDtuDriver fourGValveDtuDriver = driverMap.get(key);
        if (null == fourGValveDtuDriver) {
            synchronized(driverMap) {
                if (null == fourGValveDtuDriver) {
                    fourGValveDtuDriver = new FourGValveDtuDriver();
                    driverMap.put(key, fourGValveDtuDriver);
                }
            }
        }
        return fourGValveDtuDriver;
    }

    public void addWriteTask(Node node, RunaValve runaValve, String taskNo, String holdInfo, Integer priority) {
        String type = runaValve.getType();
        String code = null;
        switch(type) {
            case "开度":
                code = "remoteSet";
                break;
            case "回温":
                code = "returnTemp";
                break;
            case "供温":
                code = "supplyTemp";
                break;
        }
        FourGValveWriteTask fourGValveWriteTask = new FourGValveWriteTask(taskNo, holdInfo, priority, node, code, String.valueOf(runaValve.getValue()));
        getFourGValveDtuDriver(node.getSerial()).writePoints(fourGValveWriteTask);
    }

    public void addTask(Node node, String task, String holdInfo, Integer priority) {
        getFourGValveDtuDriver(node.getSerial()).readPoints(new FourGValveTask(node, task, holdInfo, priority));
    }

    public void receive(ModbusRtuResponse modbusRtuResponse) {
        FourGValveDtuDriver fourGValveDtuDriver = driverMap.get(modbusRtuResponse.getAddress());
        if (null != fourGValveDtuDriver) {
            fourGValveDtuDriver.receiveDtuResponse(modbusRtuResponse);
        }
    }

}
